<template>
  <a-modal
    title="新增房间预定"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['roomId']" /></a-form-item>
        <a-form-item
          label="預定房間"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
        <a-input  placeholder="預定房間" :disabled="true"  v-decorator="['roomCode', {rules: [{ required: true, message: '請選擇預定房間！' }]}]">
          <a-icon v-if="type === 1" slot="addonAfter" type="search" @click="onSelectRoom"/>
        </a-input>
        </a-form-item>
        <a-form-item
          label="预定人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入预定人" v-decorator="['reservation', {rules: [{required: true, message: '请输入预定人！'}]}]" />
        </a-form-item>
        <a-form-item
          label="联系方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入联系方式" v-decorator="['phone', {rules: [{required: true, message: '请输入联系方式！'}]}]" />
        </a-form-item>
        <a-form-item
          label="最晚入住日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker
          style="width: 100%"
          placeholder="请选择最晚入住日期"
          v-decorator="['latestDate',{rules: [{ required: true, message: '请选择最晚入住日期！' }]}]"
          @change="onChangelatestDate"/>
        </a-form-item>
        <a-form-item
          label="定金"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input-number
              style="width:100%"
              placeholder="請輸入每月定金"
              :precision="2"
              :min="0"
              :max="99999999"
              :step="10"
              v-decorator="['deposit', { initialValue: 0, rules: [{required: true, message: '請輸入每月定金'}]}]"/>
        </a-form-item>
        <a-form-item
          label="收款日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker
          style="width: 100%"
          :format="dateFormatList"
          placeholder="请选择收款日期"
          v-decorator="['collectionDate',{ initialValue: moment(getCurrentData(), 'YYYY-MM-DD'), rules: [{ required: true, message: '请选择收款日期！' }]}]"
          @change="onChangecollectionDate"/>
        </a-form-item>
        <a-form-item
          label="收款方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择收款方式" v-decorator="['collectionType', {rules: [{ required: true, message: '请选择收款方式！' }]}]">
            <a-select-option v-for="(item,index) in collectionTypeData" :key="index" :value="item.id">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-row :gutter="24">
              <a-form-item
              label="收租周期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input-group compact>
                <a-form-item
                    label=""
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol">
                  <a-select style="width: 150px" v-decorator="['receiptFu', { initialValue: 1, rules: [{ required: true, message: '请选择付款方式！' }]}]">
                    <a-select-option v-for="(item,index) in 18" :key="index" :value="item">付{{ item }}</a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                    label=""
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol">
                  <a-select style="width: 150px" v-decorator="['receiptYa', { initialValue: 1, rules: [{ required: true, message: '请选择押金方式！' }]}]">
                    <a-select-option v-for="(item,index) in 18" :key="index" :value="item">押{{ item }}</a-select-option>
                    <a-select-option :value="19">押金自定義</a-select-option>
                  </a-select>
                </a-form-item>
                 <a-form-item
                    label=""
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol">
                  <a-select style="width: 150px" v-decorator="['receiptYj', { initialValue: 2, rules: [{ required: true, message: '请选择付款周期！' }]}]">
                    <a-select-option :value="1">日</a-select-option>
                    <a-select-option :value="2">月</a-select-option>
                    <a-select-option :value="3">年</a-select-option>
                  </a-select>
                </a-form-item>
              </a-input-group>
            </a-form-item>
            </a-row>
            <a-form-item
              label="租金"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-input-number
              style="width:100%"
              placeholder="請輸入租金"
              :precision="2"
              :min="0"
              :max="99999999"
              :step="10"
              v-decorator="['rent', { initialValue: 0, rules: [{required: true, message: '請輸入租金'}]}]"/>
            </a-form-item>
            <a-form-item
          label="備注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="請輸入備注" v-decorator="['remark']" />
        </a-form-item>
      </a-form>
    </a-spin>
    <room-num-select ref="roomNumSelect" @ok="handleOk" />
  </a-modal>
</template>

<script>
  import { roomReservationAdd } from '@/api/modular/main/roomreservation/roomReservationManage'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  import roomNumSelect from './roomNumSelect.vue'
  import moment from 'moment'
  export default {
    components: {
     roomNumSelect
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        latestDateDateString: '',
        collectionDateDateString: '',
        collectionTypeData: [],
        dateFormatList: 'YYYY-MM-DD',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        type: 1
      }
    },
    methods: {
      moment,
      // 初始化方法
      add (type, record) {
        this.type = type
        this.visible = true
        if (type === 2) {
          setTimeout(() => {
            this.form.setFieldsValue(
              {
                roomId: record.roomId,
                roomCode: record.roomCode
              }
            )
          }, 100)
        }
        this.initNeedData()
        this.collectionDateDateString = moment().format('YYYY-MM-DD')
      },
      initNeedData() {
        busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      getCurrentData() {
        return new Date().toLocaleDateString()
      },
      onSelectRoom() {
        this.$refs.roomNumSelect.select(0)
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.latestDate = this.latestDateDateString
            values.collectionDate = this.collectionDateDateString
            roomReservationAdd(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.msg)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangelatestDate(date, dateString) {
        this.latestDateDateString = dateString
      },
      onChangecollectionDate(date, dateString) {
        this.collectionDateDateString = dateString
      },
      handleOk (roomId, roomStrName) {
        this.$nextTick(() => {
            this.form.setFieldsValue({
              roomId: roomId,
              roomCode: roomStrName
            })
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.type = 1
      }
    }
  }
</script>
